/*
 * =====================================================================================
 *
 *       Filename:  imageio.h
 *
 *    Description:  Image IO
 *
 *        Version:  1.0
 *        Created:  2009年06月13日 21时02分24秒
 *       Revision:  none
 *       Compiler:  gcc
 *
 *         Author:  Ying Wang (WY), ywang@nlpr.ia.ac.cn
 *        Company:  Institute of Automation, Chinese Academy of Sciences
 *
 * =====================================================================================
 */

#ifndef IMAGEIO_H
#define IMAGEIO_H
#include "ncmatrix.h"
#include <QtGui/QImage>
#include <vector>
typedef std::vector< NCmatrix<double> > MultiMat;
inline MultiMat imtomat(const QImage &image)
{
	MultiMat im;
	int height = image.height(), width = image.width();
	NCmatrix<double> matr(height,width),matg(height,width),matb(height,width);
	QRgb rgb;
	for(int i=0; i<height; i++ )
	{
		//rgb = (QRgb*)image.scanLine(i);
		for(int j=0; j<width; j++ )
		{
			rgb = image.pixel(j,i);
			matr[i][j] = qRed(rgb);
			matg[i][j] = qGreen(rgb);
			matb[i][j] = qBlue(rgb);
		}
	}
	im.push_back(matr);
	im.push_back(matg);
	im.push_back(matb);
	return im;
}
template<typename T>
inline QImage mattoim(const NCmatrix<T> &mat)
{
	int height = mat.row(), width = mat.column();
	QImage image(width, height, QImage::Format_RGB32);
	double tmp;
	for( int i=0; i<height; i++ )
	{
		for ( int j=0; j<width; j++ )
		{
			tmp = mat[i][j];
			image.setPixel(j,i,qRgb(tmp,tmp,tmp));
		}
	}

	return image;

}
inline QImage mattoim(const NCmatrix<double> &matr, const NCmatrix<double> &matg, const NCmatrix<double> &matb)
{
	int height = matr.row(), width = matr.column();
	QImage image(width, height, QImage::Format_RGB32);
//	double tmp;
	for(int i=0; i<height; i++ )
	{
		for (int j=0; j<width; j++ )
		{
			image.setPixel(j,i, qRgb(matr[i][j],matg[i][j],matb[i][j]));
		}
	}
	return image;
}
#endif
